package org.mzy.serve.controller;

import org.mzy.basic.util.AjaxResult;
import org.mzy.basic.util.PageList;
import org.mzy.pet.domain.Pet;
import org.mzy.serve.domain.Product;
import org.mzy.serve.query.ProductQuery;
import org.mzy.serve.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    public IProductService productService;


    /**
     * 保存和修改公用的
     *
     * @param product 传递的实体
     * @return Result转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Product product) {
        try {
            if (product.getId() != null)
                productService.update(product);
            else
                productService.add(product);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            productService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Product get(@PathVariable("id") Long id) {
        return productService.findById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<Product> list() {

        return productService.findAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<Product> json(@RequestBody ProductQuery query) {
        return productService.queryByPage(query);
    }

    /*批量删除*/
    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids) {
        productService.batchDel(ids);
        return AjaxResult.me().success();
    }

    /*上架*/
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids) {
        try {
            productService.onsale(ids);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().success(false).setMsg(e.getMessage());
        }
    }

    /*下架*/
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids) {
        try {
            productService.offsale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().success(false).setMsg(e.getMessage());
        }
    }

    /**
     * 根据Id查询宠物详情
     *
     * @param id
     * @return
     */
    @GetMapping("/getProductDetail/{id}")
    public Product getProductDetail(@PathVariable("id") Long id) {
        return productService.getProductDetail(id);
    }
}
